/**
 * Copyright (C) Coderyuan.com. All Rights Reserved.
 *
 * Created by: yuanguozheng
 * Created: 2015年4月20日 下午6:21:21
 */

package com.coderyuan.file;

import com.coderyuan.stream.StreamUtil;
import com.coderyuan.utils.SystemManager;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * ZIP压缩工具类
 *
 * @author yuanguozheng
 *
 */
public class ZipUtil {

    private static final String ZIP_FILENAME_PATTERN = "yyyyMMddHHmmss";

    private static final int BUFFER_SIZE = 1024;

    /**
     * 将文件压缩为ZIP文件
     *
     * @param 文件列表
     * @return 成功返回zip文件对象，失败返回null
     */
    public static File filesToZip(File[] files) {
        File zipFile = initZipFile();
        if ((files == null) || (files.length < 1)) {
            return null;
        }
        if (writeToZip(zipFile, files)) {
            return zipFile;
        }
        return null;
    }

    /**
     * 写入到ZIP文件
     *
     * @param ZIP文件
     * @param 需要压缩的文件
     * @return 是否压缩成功
     */
    private static boolean writeToZip(File zipFile, File[] files) {
        boolean result = false;
        FileOutputStream fos = null;
        ZipOutputStream zos = null;
        try {
            fos = new FileOutputStream(zipFile);
            zos = new ZipOutputStream(new BufferedOutputStream(fos));
            byte[] buffer = new byte[BUFFER_SIZE];
            for (File file : files) {
                ZipEntry entry = new ZipEntry(file.getName());
                zos.putNextEntry(entry);
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis, BUFFER_SIZE);
                    int read = 0;
                    while ((read = bis.read(buffer, 0, BUFFER_SIZE)) != -1) {
                        zos.write(buffer, 0, read);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    StreamUtil.closeQuietly(bis);
                    StreamUtil.closeQuietly(fis);
                }
            }
            result = true;
            return result;
        } catch (IOException e) {
            result = false;
            e.printStackTrace();
            FileUtil.del(zipFile);
        } finally {
            StreamUtil.closeQuietly(fos);
            StreamUtil.closeQuietly(zos);
        }
        return result;
    }

    /**
     * 初始化ZIP文件
     *
     * @return ZIP文件
     */
    private static File initZipFile() {
        SimpleDateFormat format = new SimpleDateFormat(ZIP_FILENAME_PATTERN, Locale.CHINA);
        String zipFileName = format.format(new Date()) + ".zip";
        File cachePath = SystemManager.getContext().getExternalCacheDir();
        File zipFile = new File(cachePath, zipFileName);
        return zipFile;
    }
}
